package com.xpsoft.oa.action.produce;


import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import com.xpsoft.core.Constants;
import com.xpsoft.core.command.QueryFilter;
import com.xpsoft.core.web.action.BaseAction;


import com.xpsoft.oa.model.produce.ArticleReceive;
import com.xpsoft.oa.service.produce.ArticleReceiveService;

import flexjson.DateTransformer;
import flexjson.JSONSerializer;
/**
 * 
 * @author 
 *
 */
public class ArticleReceiveAction extends BaseAction{
	@Resource
	private ArticleReceiveService articleReceiveService;
	private ArticleReceive articleReceive;
	
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ArticleReceive getArticleReceive() {
		return articleReceive;
	}

	public void setArticleReceive(ArticleReceive articleReceive) {
		this.articleReceive = articleReceive;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<ArticleReceive> list= articleReceiveService.getAll(filter);
		
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		
		JSONSerializer jsonSerializer = new JSONSerializer();
		jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"), "receiveDate","createTime");
		buff.append(jsonSerializer.exclude(new String[] { "class" }).include("articleReg","receiveUser","createUser").serialize(list));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	/**
	 * 批量删除
	 * @return
	 */
	public String multiDel(){
		
		String[]ids=getRequest().getParameterValues("ids");
		if(ids!=null){
			for(String id:ids){
				articleReceiveService.remove(new Long(id));
			}
		}
		
		jsonString="{success:true}";
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		ArticleReceive articleReceive=articleReceiveService.get(id);
		
		Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setDateFormat(Constants.DATE_FORMAT_YMD).create();
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(articleReceive));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	public String getByReg(){
		
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		JSONSerializer jsonSerializer = new JSONSerializer();
		jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"), "receiveDate","createTime");
		String regId = getRequest().getParameter("regId");
		if(StringUtils.isNotBlank(regId)){
			ArticleReceive articleReceive = articleReceiveService.getByRegId(Long.valueOf(regId));
			if(articleReceive!=null){
				sb.append(jsonSerializer.exclude(new String[] { "class" }).include("articleReg","receiveUser","createUser").serialize(articleReceive));
			}else{
				sb.append("''");
			}
		}else{
			sb.append("''");
		}
		sb.append("}");
		setJsonString(sb.toString());
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		articleReceiveService.save(articleReceive);
		setJsonString("{success:true}");
		return SUCCESS;
	}
}
